package com.olocat.cjx.nsims.controller

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.olocat.cjx.nsims.dao.AdviceRepository
import com.olocat.cjx.nsims.dao.PatientRepository
import com.olocat.cjx.nsims.pojo.info.Advice
import com.olocat.cjx.nsims.pojo.personnel.Doctor
import com.olocat.cjx.nsims.pojo.personnel.Patient
import com.olocat.cjx.nsims.service.NurseService
import com.olocat.cjx.nsims.service.PatientService
import com.olocat.cjx.nsims.util.Response
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource

@RestController
class PatientController {

    @Resource
    lateinit var patientService: PatientService
    @Resource
    lateinit var nurseService: NurseService
    @Resource
    lateinit var adviceRepository: AdviceRepository

    @Resource
    lateinit var patientRepository: PatientRepository
    val log = LoggerFactory.getLogger(NurseController::class.java)!!

    @GetMapping("/api/patient")
    fun get(id: String?, department: Int?, roleId:String?): Response {
        if(roleId != null) {
            val nurse = nurseService.getById(roleId)
        }
        return if (id == null) {
            if (department == null)
                Response.builder(patientService.list())
            else Response.builder(patientService.getPatientsByDepartment(department))
        } else {
            println(patientService.getById(id))
            Response.builder(patientService.getById(id))
        }
    }
    @GetMapping("/api/patient/nurse")
    fun getPatientByNurse(nurse:String,department: Int,role: String): Response{
        println(role);
        println(nurse);
        println(department);
        return if (role == "doctor"){
            Response.builder(patientService.getPatientsByDepartment(department))
        }else{
            Response.builder(patientService.getPatientsByNurse(nurse))
        }
    }
    @GetMapping("/api/patient/ward")
    fun getPatientByWard(number:String?): Response{
        return if (number == null){
            Response.builder(patientService.list())
        }else {
                Response.builder(patientRepository.getPatientsByWard(number))
        }
    }
    @PostMapping("api/patient")
    fun add(@RequestBody json: String): Response {
        val patient = JSON.parseObject(json, Patient::class.java)
        patientService.save(patient)
        return Response.builder()
    }

    @DeleteMapping("/api/patient")
    fun del(id: String): Response {
        patientService.removeById(id)
        return Response.builder()
    }

    @GetMapping("/api/patient/out")
    fun out(id: String): Response {
        val patient = patientService.getById(id)
        patient.state = 2
        patientService.updateById(patient)
        return Response.builder()
    }

    @PostMapping("/api/patient/update")
    fun update(@RequestBody json: String?): Response {
        println(json)
        val patient = JSON.parseObject(json, Patient::class.java)
        patientService.updateById(patient)
        return Response.builder()
    }

    @PostMapping("/api/patient/save")
    fun save(@RequestBody json: String): Response {
        val patient = JSON.parseObject(json, Patient::class.java)
        patientService.updateById(patient)
        return Response.builder()
    }
    @GetMapping("/api/patient/query")
    fun getByName (name:String?):Response{
        log.info("姓名查询 $name")
        return if (name == null) {
            log.info("name is null")
            return Response.builder()
        } else {
            Response.builder(patientRepository.getByName(name))
        }
    }
    @PostMapping("/api/patient/advice")
    fun addAdvice(@RequestBody json: String): Response {
        println(json)
        val advice = JSON.parseObject(json, Advice::class.java)
        adviceRepository.save(advice)
        advice.patientId?.let { patientService.setAdviceStateById(it, advice.state) }
        return Response.builder()
    }

    @GetMapping("/api/patient/advice/do")
    fun doAdvice(id: Int): Response {
        val advice = adviceRepository.getOne(id)
        advice.patientId?.let {
            advice.state++
            patientService.setAdviceStateById(it, advice.state)
            adviceRepository.save(advice)
        }
        return Response.builder()
    }
}